package com.hotec.PROJECT_NAME.utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.hotec.PROJECT_NAME.component.enums.SmsEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author 江南女鬼
 * @date 2017/8/10
 */
@Slf4j
public class UtilsSms {
    private static Random random = RandomUtil.getRandom();

    public static String getCode(String mobile, Enum<SmsEnum> smsEnum) {
        StringRedisTemplate redisTemplate = UtilsSpring.getBean(StringRedisTemplate.class);
        return redisTemplate.opsForValue().get(smsEnum.name() + ":" + mobile);
    }

    public static String getCode(String key) {
        StringRedisTemplate redisTemplate = UtilsSpring.getBean(StringRedisTemplate.class);
        return redisTemplate.opsForValue().get(key);
    }


    /**
     * 验证短信
     */
    public static boolean valid(String mobile, String vcode, Enum<SmsEnum> smsEnum) {
        //如果appstore验证用的
        if (StrUtil.equalsIgnoreCase(vcode, "testSmsCode")) {
            return true;
        }
        String code = getCode(mobile, smsEnum);
        return !(StrUtil.isBlank(code) || StrUtil.isBlank(vcode)) && code.equals(vcode);
    }

    /**
     * 验证短信
     */
    public static void validImgCode(String mobile, String vcode, String type) {
        String key = StrUtil.join(":", type, mobile);

        Assert.notBlank(vcode, "验证码不能为空");

        String code = Assert.notBlank(getCode(key), "图形验证码已失效");


        Assert.isTrue(vcode.equalsIgnoreCase(code), "图形验证码不正确");
    }

    public static void getImgCode(String type, String mobile, HttpServletResponse response) {
        BufferedImage bufferedImage = getMsg(StrUtil.join(":", type, mobile));

        try {
            ImageIO.write(bufferedImage, "JPEG", response.getOutputStream());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException e) {
            UtilsLog.log(UtilsSms.class, e);
        }
    }

    private static BufferedImage getMsg(String key){
        int width = 60 , height = 30;
        //创建一个图像，宽60 高30
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(getRandomColor(200,250));
        g.fillRect(0,0,width,height);
        g.setFont(new Font("Times New Roman",Font.PLAIN,18));
        g.setColor(getRandomColor(160,200));
        //干扰线生成
        for (int i = 0; i < 10; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x,y,x+xl,y+yl);
        }
        String strCode = RandomUtil.randomString(4);

        String[] split = StrUtil.split(strCode, 1);

        for (int i = 0; i < split.length; i++) {
            String strNumber = split[i];
            //设置字体颜色
            g.drawString(strNumber,13*i+6,20);
        }

        StringRedisTemplate redisTemplate = UtilsSpring.getBean(StringRedisTemplate.class);

        redisTemplate.delete(key);

        redisTemplate.opsForValue().set(key, strCode, 60L, TimeUnit.SECONDS);

        g.dispose();

        return image;
    }

    /**
     * 随机获取颜色的方法
     * @return
     */
    private static Color getRandomColor (int fc , int bc){
        Color reandomColor = null;
        if(fc > 255){
            fc = 255;
        }
        if(bc > 255){
            bc = 255;
        }
        int r =fc + random.nextInt(bc-fc);
        int g = fc + random.nextInt(bc-fc);
        int b = fc + random.nextInt(bc-fc);
        reandomColor = new Color(r,g,b);
        return reandomColor;
    }

    public static void removeValidImgCode(String mobile, String type) {
        StringRedisTemplate redisTemplate = UtilsSpring.getBean(StringRedisTemplate.class);

        redisTemplate.delete(StrUtil.join(":", type, mobile));
    }
}
